package game;

import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class LevelMenu extends Menu {
	
	public LevelMenu(Menu a){
		super(a);
		init();
	}
	
	private void init(){
		elements.add(new MenuButton(this, 500, 300, 200, 50, "Load Level", 30, null){
			public void clicked(MouseEvent e){
				JFileChooser fc = new JFileChooser(new File("levels"));
				LvlFileFilter filter = new LvlFileFilter();
                fc.addChoosableFileFilter(filter);
                fc.setFileFilter(filter);
                fc.setAcceptAllFileFilterUsed(false);
                int val = fc.showOpenDialog(null);
                if(val == fc.APPROVE_OPTION && filter.accept(fc.getSelectedFile())){
                    Level lvl = new Level(getCanvas(), fc.getSelectedFile());
                    host.canvas.level = lvl;
                    boolean succ = host.canvas.level.read();
                    if(succ)
                    	JOptionPane.showMessageDialog(host.canvas, "Level Loaded");
                    else
                    	JOptionPane.showMessageDialog(host.canvas, "Error", "Level Not Found", 3);
                    getCanvas().resumeGame();
                    parent.subMenu = null;
                }                
			}
		});
		elements.add(new MenuButton(this, 750, 300, 200, 50, "Save Level", 30, null){
			public void clicked(MouseEvent e){
				host.canvas.level.save();
				JOptionPane.showMessageDialog(host.canvas, "Level Saved");
			}
		});
		elements.add(new MenuButton(this, 500, 375, 200, 50, "Save As...", 30, null){
			public void clicked(MouseEvent e){
				JFileChooser window = new JFileChooser(new File("levels"));
				LvlFileFilter filter = new LvlFileFilter();
		        window.addChoosableFileFilter(filter);
		        window.setFileFilter(filter);
		        window.setAcceptAllFileFilterUsed(false);
				int r = window.showSaveDialog(null);
				try {
					if (r == JFileChooser.APPROVE_OPTION) {
						File s = window.getSelectedFile();
						File file = new File(s.toString());
						host.canvas.level.saveAs(s + ".lvl");
						if(file.createNewFile()== true){
							JOptionPane.showMessageDialog(window,"Level Created");
						}else{
							JOptionPane.showMessageDialog(window, "Level Overwritten");
						}
					}
				}catch (IOException e1) {}
			}
		});
		elements.add(new MenuButton(this, 750, 375, 200, 50, "New Level", 30, null){
			public void clicked(MouseEvent e){
				String name = JOptionPane.showInputDialog(canvas, "Enter file name:  ", "New Level", JOptionPane.PLAIN_MESSAGE);
				File s = new File("levels/"+name+".lvl");
				Level l = new Level(canvas, s);
				getCanvas().level = l;
			}
		});
		elements.add(new MenuButton(this, 500, 450, 450, 50, "Back", 30, null){
			public void clicked(MouseEvent e){
				if(parent != null){
					parent.subMenu = null;
				}
			}
		});
		
	}
}
